﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

using Cn.Edwon.Biz;
using Cn.Edwon.Spring;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Web.Session;
using Cn.Edwon.Web;
using Cn.Cellula.Model;
using Cn.Cellula.Model.Security;
using Cn.Cellula.Client;
using System.Reflection;

namespace Cn.Cellula.MiniTopSite {
    public class Global : System.Web.HttpApplication {
        private static Common.Logging.ILog log = Common.Logging.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);


        protected void Application_Start(object sender, EventArgs e) {

        }

        protected void Session_Start(object sender, EventArgs e) {
            log.Debug("Session start....");
            if (Session[ClientHelper.SESS_USER_KEY] == null) {
                log.Debug("Session not exists");
                ClientBiz.CookieItem ci = ClientBiz.Cookie.Load(Request.Cookies[ClientBiz.Cookie.COOKIE_NAME], Request.UserHostAddress);
                if (ci != null) {
                    log.Debug("CookieItem is not null");
                    ISessActor actor = new SessActor();
                    try {
                        IUserBiz biz = BizFactory.Get<IUserBiz>();
                        VSysUserEO eo = biz.Load<VSysUserEO>(ci.UserKey);
                        log.Debug(eo.LoginName + " login with cookie:" + ClientBiz.Cookie.COOKIE_NAME);
                        if (ci.Password == ClientBiz.Md5(eo.Password + ClientBiz.Cookie.COOKIE_SECRET_KEY)) {
                            new LoginHelper().BindLoginData(eo, ref actor);
                            if (actor != null) {
                                actor.LastIP = Request.UserHostAddress;
                                Session[ClientHelper.SESS_USER_KEY] = actor;
                                log.Debug("Session build succeed by cookie");
                            }
                        } else {
                            log.Debug("Cookie password invalid");
                        }
                    } catch {
                        log.Error("load user failed from cookie,invalid userkey:" + ci.UserKey);
                    }
                } else {
                    log.Debug("CookieItem is null");
                }
            } else {
                log.Debug("Session exists");
            }
            log.Debug("Session started");
        }

        protected void Application_BeginRequest(object sender, EventArgs e) {
            if (Request.ContentLength > 4096000) {
                Response.Redirect("error.aspx?key=410");
            }
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e) {

        }

        protected void Application_Error(object sender, EventArgs e) {

        }

        protected void Session_End(object sender, EventArgs e) {

        }

        protected void Application_End(object sender, EventArgs e) {

        }
    }
}